﻿Namespace XMLOperations
    Public Class XMLTranslator
        Private Const FileMajorVersion = 1
        Private Const FileMinorVersion = 9
        Public Event ProjectConvertedToXProject()
        Public Function XMLStructureToL3DPlatformPlan(ByVal File As XElement) As L3DObjectsLibrary.L3DPlatformPlan
            Dim PlatformPlan As New L3DObjectsLibrary.L3DPlatformPlan
            With PlatformPlan
                .CreateControl()
                .Clear()
                .PresentationModel = File.@Model
                .PresentationOwner = File.@Owner
                For Each XProject As XElement In (From el In File.<project> Select el)
                    PlatformPlan.Projects.Add(XProjectToProject(XProject))
                Next
            End With
            Return PlatformPlan
        End Function
        Private Function XProjectToProject(ByVal XProject As XElement) As L3DObjectsLibrary.Project
            Dim Project As New L3DObjectsLibrary.Project
            Dim VisibleMilestones As L3DObjectsLibrary.VisibleMilestonesStructure
            Dim PEl As L3DObjectsLibrary.ProjectElement = Nothing
            Dim ElList As List(Of XElement) = (From el In XProject.<projectelement> Select el).ToList
            For Each XProjectElement As XElement In ElList
                PEl = XProjectElementToProjectElement(XProjectElement)
                If PEl.HasSpecialIDMilestones <> L3DObjectsLibrary.Milestone.SpecialIDValues.None Then
                    For Each El As L3DObjectsLibrary.ProjectElement In Project.ProjectElements
                        If El.HasSpecialIDMilestones = PEl.HasSpecialIDMilestones Then
                            Project.ProjectElements.Remove(El)
                            Exit For
                        End If
                    Next
                End If
                Project.ProjectElements.Add(PEl)
            Next
            If Project.GetSpecialMilestone(L3DObjectsLibrary.Milestone.SpecialIDValues.SOP) Is Nothing Then
                Project.InsertSpecialMilestone(L3DObjectsLibrary.Milestone.SpecialIDValues.SOP)
            End If
            If Project.GetSpecialMilestone(L3DObjectsLibrary.Milestone.SpecialIDValues.DAP) Is Nothing Then
                Project.InsertSpecialMilestone(L3DObjectsLibrary.Milestone.SpecialIDValues.DAP)
            End If
            With XProject
                Project.L3Owner = .@L3Owner
                Project.ApplicationOwner = .@ApplicationOwner
                Project.Model = .@Model
                Project.Name = .@Name
                Project.ProjectRisk = .@ProjectRisk
                Project.ProjectHighlights = .@ProjectHilights
                If .@VisibleDap = "" Then
                    VisibleMilestones.DAP = True
                Else
                    VisibleMilestones.DAP = .@VisibleDap
                End If
                If .@VisibleSop = "" Then
                    VisibleMilestones.SOP = True
                Else
                    VisibleMilestones.SOP = .@VisibleSop
                End If
                If .@ProjectDAPDate = "" Then
                    Project.ProjectDAPDate = Project.GetSpecialDate("dap")
                Else
                    Project.ProjectDAPDate = DateTime.Parse(Left(.@ProjectDAPDate, 10))
                End If
                If .@ProjectSOPDate = "" Then
                    Project.ProjectSOPDate = Project.GetSpecialDate("sop")
                Else
                    Project.ProjectSOPDate = DateTime.Parse(Left(.@ProjectSOPDate, 10))
                End If
                Project.VisibleMilestones = VisibleMilestones
            End With
            Return Project
        End Function
        Private Function XProjectElementToProjectElement(ByVal XProjectElement As XElement) As L3DObjectsLibrary.ProjectElement
            Dim ProjectElement As New L3DObjectsLibrary.ProjectElement
            With XProjectElement
                ProjectElement.Name = .@Name
            End With
            For Each XMilestone As XElement In (From el In XProjectElement.<milestone>)
                Dim Mil As L3DObjectsLibrary.Milestone = XMilestoneToMilestone(XMilestone)
                ProjectElement.Milestones.Add(Mil)
            Next
            For Each XGroup As XElement In (From el In XProjectElement.<group>)
                ProjectElement.Groups.Add(XGroupToGroup(XGroup))
            Next
            Return ProjectElement
        End Function
        Private Function XMilestoneToMilestone(ByVal XMilestone As XElement) As L3DObjectsLibrary.Milestone
            Dim Milestone As New L3DObjectsLibrary.Milestone
            With XMilestone
                Milestone.Name = .@Name
                Milestone.MilestoneDate = DateTime.Parse(Left(.@MilestoneDate, 10))
                Milestone.MilestoneRealDate = DateTime.Parse(Left(.@MilestoneRealDate, 10))
                Milestone.Remark = .@Remark
                If .@Editable = "" Then
                    Milestone.Editable = True
                Else
                    Milestone.Editable = .@Editable
                End If
                If .@Visible = "" Then
                    Milestone.ShowInPlan = True
                Else
                    Milestone.ShowInPlan = .@ShowInPlan
                End If
                If .@SpecialID = "" Then
                    Milestone.SpecialID = L3DObjectsLibrary.Milestone.SpecialIDValues.None
                Else
                    Milestone.SpecialID = .@SpecialID
                End If
            End With
            Return Milestone
        End Function
        Private Function XGroupToGroup(ByVal XGroup As XElement) As L3DObjectsLibrary.Group
            Dim Group As New L3DObjectsLibrary.Group
            With XGroup
                Group.Name = .@Name
            End With
            For Each XCar As XElement In (From el In XGroup.<car>)
                Group.Cars.Add(XCarToCar(XCar))
            Next
            Return Group
        End Function
        Private Function XCarToCar(ByVal XCar As XElement) As L3DObjectsLibrary.Car
            Dim Car As New L3DObjectsLibrary.Car
            With XCar
                Car.Name = .@Name
                Car.EOBD = .@EOBD
                Car.CarStartDate = DateTime.Parse(Left(.@StartDate, 10))
                Car.CarRealStartDate = DateTime.Parse(Left(.@RealStartDate, 10))
                Car.CarEndDate = DateTime.Parse(Left(.@EndDate, 10))
                Car.CarRealEndDate = DateTime.Parse(Left(.@RealEndDate, 10))
                Car.kmHS = .@KmHS
                Car.kmCity = .@KmCity
                Car.kmRG = .@KmRG
                Car.kmStatus = .@KmStatus
                Car.HSSpeed = .@HSSpeed
                Car.RGSpeed = .@RGSpeed
                Car.CitySpeed = .@CitySpeed
            End With
            Return Car
        End Function
        Public Function L3dPlatformPlanToXML(ByVal PresentationObject As L3DObjectsLibrary.L3DPlatformPlan) As XElement
            Dim XFile As XElement = <model/>
            With XFile
                .@FileMajorVersion = FileMajorVersion
                .@FileMinorVersion = FileMinorVersion
                .@Model = PresentationObject.PresentationModel
                .@Owner = PresentationObject.PresentationOwner
                For Each Project As L3DObjectsLibrary.Project In PresentationObject.Projects
                    .Add(L3DProjectStructure(Project))
                Next
            End With
            Return XFile
        End Function
        Private Function L3DProjectStructure(ByVal Project As L3DObjectsLibrary.Project) As XElement
            Dim Struttura As XElement = <project/>
            With Struttura
                .@L3Owner = Project.L3Owner
                .@ApplicationOwner = Project.ApplicationOwner
                .@Model = Project.Model
                .@Name = Project.Name
                .@ProjectStart = Format(Project.ProjectStartDate, "dd/MM/yyyy")
                .@ProjectRisk = Project.ProjectRisk
                .@ProjectHilights = Project.ProjectHighlights
                .@ProjectDAPDate = Format(Project.ProjectDAPDate, "dd/MM/yyyy")
                .@ProjectSOPDate = Format(Project.ProjectSOPDate, "dd/MM/yyyy")
                .@VisibleDap = Project.VisibleMilestones.DAP
                .@VisibleSop = Project.VisibleMilestones.SOP
            End With
            For Each Elemento As L3DObjectsLibrary.ProjectElement In Project.ProjectElements
                Struttura.Add(L3DProjectElementStructure(Elemento))
            Next
            RaiseEvent ProjectConvertedToXProject()
            Return Struttura
        End Function
        Private Function L3DProjectElementStructure(ByVal ProjectElement As L3DObjectsLibrary.ProjectElement) As XElement
            Dim Struttura As XElement = <projectelement/>
            With Struttura
                .@Name = ProjectElement.Name
            End With
            For Each Milestone As L3DObjectsLibrary.Milestone In ProjectElement.Milestones
                Struttura.Add(L3dMilestoneStructure(Milestone))
            Next
            For Each Group As L3DObjectsLibrary.Group In ProjectElement.Groups
                Struttura.Add(L3DGroupStructure(Group))
            Next
            Return Struttura
        End Function
        Private Function L3dMilestoneStructure(ByVal Milestone As L3DObjectsLibrary.Milestone) As XElement
            Dim Struttura As XElement = <milestone/>
            With Struttura
                .@Name = Milestone.Name
                .@MilestoneDate = Format(Milestone.MilestoneDate, "dd/MM/yyyy")
                .@MilestoneRealDate = Format(Milestone.MilestoneRealDate, "dd/MM/yyyy")
                .@Remark = Milestone.Remark
                .@Editable = Milestone.Editable
                .@ShowInPlan = Milestone.ShowInPlan
                .@SpecialID = Milestone.SpecialID
            End With
            Return Struttura
        End Function
        Private Function L3DGroupStructure(ByVal Group As L3DObjectsLibrary.Group) As XElement
            Dim Struttura As XElement = <group/>
            With Struttura
                .@Name = Group.Name
            End With
            For Each Car As L3DObjectsLibrary.Car In Group.Cars
                Struttura.Add(L3DCarStructure(Car))
            Next
            Return Struttura
        End Function
        Private Function L3DCarStructure(ByVal Car As L3DObjectsLibrary.Car) As XElement
            Dim Struttura As XElement = <car/>
            With Struttura
                .@Name = Car.Name
                .@EOBD = Car.EOBD
                .@StartDate = Format(Car.CarStartDate, "dd/MM/yyyy")
                .@RealStartDate = Format(Car.CarRealStartDate, "dd/MM/yyyy")
                .@EndDate = Format(Car.CarEndDate, "dd/MM/yyyy")
                .@RealEndDate = Format(Car.CarRealEndDate, "dd/MM/yyyy")
                .@KmHS = Car.kmHS
                .@KmCity = Car.kmCity
                .@KmRG = Car.kmRG
                .@KmStatus = Car.kmStatus
                .@HSSpeed = Car.HSSpeed
                .@RGSpeed = Car.RGSpeed
                .@CitySpeed = Car.CitySpeed
            End With
            Return Struttura
        End Function
    End Class
End Namespace
